```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深度解析Java ArrayList源码</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .article-title {
            font-family: 'Noto Serif SC', serif;
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #4a6baf;
            color: #333;
        }
        .highlight-box {
            background-color: #f0f7ff;
            border-left: 4px solid #4a6baf;
        }
        .tip-box {
            background-color: #f8f9fa;
            border-left: 4px solid #6c757d;
        }
        .warning-box {
            background-color: #fff3cd;
            border-left: 4px solid #ffc107;
        }
        .diagram-container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="bg-gradient-to-r from-blue-600 to-indigo-800 text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="article-title text-4xl md:text-5xl font-bold leading-tight mb-4">深度解析Java ArrayList源码</h1>
            <p class="text-xl md:text-2xl text-blue-100 mb-8">揭开Java最常用集合类的实现奥秘</p>
            <div class="flex justify-center space-x-4">
                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-500 text-white">
                    <i class="fas fa-code mr-1"></i> Java集合
                </span>
                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-indigo-500 text-white">
                    <i class="fas fa-layer-group mr-1"></i> 源码解析
                </span>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                在Java开发中，ArrayList是我们最常用的集合类之一，它提供了动态数组的实现，使得我们可以方便地存储和操作对象集合。无论是日常业务开发还是算法实现，ArrayList都是不可或缺的工具。然而，很多开发者虽然经常使用它，却对其内部实现机制知之甚少。
            </p>
            <div class="flex items-center text-gray-500 mt-8">
                <i class="fas fa-clock mr-2"></i>
                <span>阅读时间约15分钟</span>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <h2 class="article-title text-3xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">1. ArrayList的基础架构</h2>
            
            <!-- Subsection 1.1 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">1.1 类继承结构</h3>
                <p class="text-gray-700 mb-4">让我们看看ArrayList的类定义：</p>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt;
        implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable</code></pre>
                </div>
                <p class="text-gray-700 mb-4">从这个定义中，我们可以看出：</p>
                <ul class="list-disc pl-6 mb-6 text-gray-700 space-y-2">
                    <li>ArrayList继承自AbstractList，获得了List接口的默认实现</li>
                    <li>实现了RandomAccess接口，表明它支持快速随机访问</li>
                    <li>实现了Cloneable接口，可以被克隆</li>
                    <li>实现了Serializable接口，可以被序列化</li>
                </ul>
            </div>

            <!-- Subsection 1.2 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">1.2 核心成员变量</h3>
                <p class="text-gray-700 mb-4">ArrayList的核心在于其内部的数组实现，让我们看看关键的成员变量：</p>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">/**
 * 存储ArrayList元素的数组缓冲区
 */
transient Object[] elementData;

/**
 * ArrayList包含的元素数量
 */
private int size;

/**
 * 默认初始容量
 */
private static final int DEFAULT_CAPACITY = 10;

/**
 * 用于空实例的共享空数组实例
 */
private static final Object[] EMPTY_ELEMENTDATA = {};

/**
 * 用于默认大小的空实例的共享空数组实例
 * 我们把它和EMPTY_ELEMENTDATA区分开，以便知道在添加第一个元素时要膨胀多少
 */
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};</code></pre>
                </div>
                <p class="text-gray-700 mb-4">这些变量揭示了ArrayList的核心机制：</p>
                <ul class="list-disc pl-6 mb-6 text-gray-700 space-y-2">
                    <li>使用Object数组存储元素</li>
                    <li>通过size变量跟踪实际元素数量</li>
                    <li>定义了默认初始容量为10</li>
                    <li>针对不同的空列表情况使用不同的空数组实例</li>
                </ul>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-16">
            <h2 class="article-title text-3xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">2. ArrayList的构造方法</h2>
            <p class="text-gray-700 mb-6">ArrayList提供了三种构造方法，让我们逐一分析：</p>
            
            <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                <pre><code class="text-sm">/**
 * 创建一个指定初始容量的空列表
 */
public ArrayList(int initialCapacity) {
    if (initialCapacity > 0) {
        this.elementData = new Object[initialCapacity];
    } else if (initialCapacity == 0) {
        this.elementData = EMPTY_ELEMENTDATA;
    } else {
        throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
    }
}

/**
 * 创建一个空列表，初始容量为10
 */
public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

/**
 * 创建一个包含指定集合元素的列表
 */
public ArrayList(Collection&lt;? extends E&gt; c) {
    elementData = c.toArray();
    if ((size = elementData.length) != 0) {
        // c.toArray可能返回的不是Object[]类型
        if (elementData.getClass() != Object[].class) {
            elementData = Arrays.copyOf(elementData, size, Object[].class);
        }
    } else {
        // 如果集合为空，使用空数组
        this.elementData = EMPTY_ELEMENTDATA;
    }
}</code></pre>
            </div>
            
            <div class="highlight-box p-4 rounded-lg mb-6">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-blue-500 mr-3 mt-1">
                        <i class="fas fa-info-circle"></i>
                    </div>
                    <div>
                        <h4 class="font-semibold text-gray-800 mb-2">重要提示</h4>
                        <p class="text-gray-700">无参构造方法并不会立即创建容量为10的数组，而是使用一个空数组，只有在第一次添加元素时才会扩容到10。当使用已有集合初始化时，会复制该集合的元素到内部数组。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-16">
            <h2 class="article-title text-3xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">3. 动态扩容机制</h2>
            <p class="text-gray-700 mb-6">ArrayList最重要的特性就是其动态扩容机制，让我们深入研究它是如何实现的。</p>
            
            <!-- Subsection 3.1 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">3.1 添加元素时的扩容逻辑</h3>
                <p class="text-gray-700 mb-4">当我们调用<code class="bg-gray-100 px-1 py-0.5 rounded">add(E e)</code>方法时，ArrayList会首先确保内部数组有足够的空间：</p>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // 确保有足够空间
    elementData[size++] = e;
    return true;
}

private void ensureCapacityInternal(int minCapacity) {
    ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}

private static int calculateCapacity(Object[] elementData, int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        return Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    return minCapacity;
}

private void ensureExplicitCapacity(int minCapacity) {
    modCount++;

    // 如果需要的容量超过了当前数组的长度，进行扩容
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}</code></pre>
                </div>
            </div>

            <!-- Subsection 3.2 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">3.2 核心扩容方法grow</h3>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">/**
 * 增加容量，确保至少能容纳最小容量参数指定的元素数量
 */
private void grow(int minCapacity) {
    // 获取旧容量
    int oldCapacity = elementData.length;
    // 新容量 = 旧容量 + 旧容量/2，即扩容约50%
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    // 如果新容量仍小于最小需要容量，则直接使用最小需要容量
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    // 如果新容量超过MAX_ARRAY_SIZE，则调用hugeCapacity
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // 复制旧数组元素到新数组
    elementData = Arrays.copyOf(elementData, newCapacity);
}</code></pre>
                </div>
                <p class="text-gray-700 mb-4">从这段代码中，我们可以看出:</p>
                <ol class="list-decimal pl-6 mb-6 text-gray-700 space-y-2">
                    <li>ArrayList每次扩容约为原容量的1.5倍（oldCapacity + oldCapacity >> 1）</li>
                    <li>扩容通过创建新数组并复制旧数组元素完成</li>
                    <li>有最大容量限制，超过阈值将使用hugeCapacity方法处理</li>
                </ol>
            </div>

            <!-- Subsection 3.3 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">3.3 图解扩容过程</h3>
                <p class="text-gray-700 mb-4">以下是ArrayList扩容过程的简化图示：</p>
                
                <div class="bg-white p-6 rounded-lg shadow-md mb-6">
                    <h4 class="font-semibold text-gray-800 mb-3">初始状态（使用默认构造函数后添加第一个元素）：</h4>
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre><code class="text-sm">[e, _, _, _, _, _, _, _, _, _]
 |
size=1, capacity=10</code></pre>
                    </div>
                    
                    <h4 class="font-semibold text-gray-800 mb-3">添加元素直到数组填满：</h4>
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre><code class="text-sm">[e1, e2, e3, e4, e5, e6, e7, e8, e9, e10]
                                      |
                          size=10, capacity=10</code></pre>
                    </div>
                    
                    <h4 class="font-semibold text-gray-800 mb-3">添加第11个元素，触发扩容：</h4>
                    <div class="code-block p-4 rounded-lg overflow-x-auto">
                        <pre><code class="text-sm">[e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, _, _, _, _]
                                           |
                               size=11, capacity=15</code></pre>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="diagram-container mb-6">
                    <div class="mermaid">
                        graph TD
                            A[创建ArrayList] -->|默认构造函数| B[空数组DEFAULTCAPACITY_EMPTY_ELEMENTDATA]
                            B -->|添加第一个元素| C[扩容到DEFAULT_CAPACITY=10]
                            C -->|继续添加元素| D[数组填满 size=10]
                            D -->|添加第11个元素| E[扩容到15 (10 + 10/2)]
                            E -->|继续添加元素| F[数组填满 size=15]
                            F -->|添加第16个元素| G[扩容到22 (15 + 15/2)]
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-16">
            <h2 class="article-title text-3xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">4. ArrayList的核心方法源码分析</h2>
            <p class="text-gray-700 mb-6">除了扩容机制外，ArrayList还有许多重要的方法，让我们分析几个关键方法的源码。</p>
            
            <!-- Subsection 4.1 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">4.1 随机访问方法get与set</h3>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public E get(int index) {
    rangeCheck(index);
    return elementData(index);
}

public E set(int index, E element) {
    rangeCheck(index);
    E oldValue = elementData(index);
    elementData[index] = element;
    return oldValue;
}

E elementData(int index) {
    return (E) elementData[index];
}

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}</code></pre>
                </div>
                <div class="tip-box p-4 rounded-lg mb-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 text-gray-600 mr-3 mt-1">
                            <i class="fas fa-lightbulb"></i>
                        </div>
                        <div>
                            <p class="text-gray-700">这些方法非常简单，直接对内部数组进行操作，时间复杂度为O(1)。这就是为什么ArrayList支持高效随机访问的原因。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Subsection 4.2 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">4.2 删除元素方法remove</h3>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public E remove(int index) {
    rangeCheck(index);
    modCount++;
    E oldValue = elementData(index);
    
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index, numMoved);
    
    elementData[--size] = null; // 清除引用，帮助GC
    return oldValue;
}

public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index, numMoved);
    elementData[--size] = null;
}</code></pre>
                </div>
                <p class="text-gray-700 mb-4">从源码可以看出：</p>
                <ol class="list-decimal pl-6 mb-6 text-gray-700 space-y-2">
                    <li>删除元素时，需要将删除位置之后的所有元素向前移动一位</li>
                    <li>最后将数组末尾多余的引用置为null，帮助GC回收</li>
                    <li>删除操作的时间复杂度与元素位置相关，最坏情况为O(n)</li>
                </ol>
            </div>

            <!-- Subsection 4.3 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">4.3 批量操作方法</h3>
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public boolean addAll(Collection&lt;? extends E&gt; c) {
    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // 确保容量足够
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    return numNew != 0;
}

public boolean removeAll(Collection&lt;?&gt; c) {
    return batchRemove(c, false);
}

public boolean retainAll(Collection&lt;?&gt; c) {
    return batchRemove(c, true);
}

private boolean batchRemove(Collection&lt;?&gt; c, boolean complement) {
    final Object[] elementData = this.elementData;
    int r = 0, w = 0;
    boolean modified = false;
    try {
        for (; r < size; r++)
            if (c.contains(elementData[r]) == complement)
                elementData[w++] = elementData[r];
    } finally {
        // 即使出现异常，也要保证数组一致性
        if (r != size) {
            System.arraycopy(elementData, r, elementData, w, size - r);
            w += size - r;
        }
        if (w != size) {
            for (int i = w; i < size; i++)
                elementData[i] = null;
            modCount += size - w;
            size = w;
            modified = true;
        }
    }
    return modified;
}</code></pre>
                </div>
                <p class="text-gray-700 mb-4">这些批量操作方法展示了ArrayList处理大量元素的策略，以及如何保证操作的原子性和数据的一致性。</p>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-16">
            <h2 class="article-title text-3xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">5. 常见问题</h2>
            <p class="text-gray-700 mb-6">基于对ArrayList源码的深入分析，以下是一些实用建议和常见陷阱的避免方法：</p>
            
            <!-- Subsection 5.1 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">5.1 容量管理</h3>
                <div class="flex items-start mb-6">
                    <div class="flex-shrink-0 text-yellow-500 mr-3 mt-1">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <div>
                        <ul class="list-disc pl-6 text-gray-700 space-y-2">
                            <li>如果预先知道需要存储元素的大致数量，使用带初始容量的构造函数</li>
                            <li>当需要添加大量元素时，考虑使用<code class="bg-gray-100 px-1 py-0.5 rounded">ensureCapacity(int minCapacity)</code>方法预先扩容</li>
                        </ul>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-6">
                    <div class="warning-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">不好的实践</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
for (int i = 0; i < 10000; i++) {
    list.add(i);
}</code></pre>
                        </div>
                    </div>
                    <div class="highlight-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">好的实践</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(10000);
for (int i = 0; i < 10000; i++) {
    list.add(i);
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Subsection 5.2 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">5.2 避免频繁的中间位置插入和删除</h3>
                <p class="text-gray-700 mb-4">ArrayList在中间位置插入或删除元素时需要移动大量元素，性能较差。</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-6">
                    <div class="warning-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">性能较差的代码</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
// ... 添加大量元素
for (int i = 0; i < 1000; i++) {
    list.add(0, i); // 每次都在开头插入，导致所有元素后移
}</code></pre>
                        </div>
                    </div>
                    <div class="highlight-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">更好的选择</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">LinkedList&lt;Integer&gt; linkedList = new LinkedList&lt;&gt;();
// ... 添加大量元素
for (int i = 0; i < 1000; i++) {
    linkedList.addFirst(i); // LinkedList在开头插入效率高
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Subsection 5.3 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">5.3 注意线程安全问题</h3>
                <p class="text-gray-700 mb-4">ArrayList不是线程安全的，多线程环境下直接使用可能导致数据不一致。</p>
                
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">// 线程不安全
ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();

// 线程安全的替代方案
List&lt;Integer&gt; safeList = Collections.synchronizedList(new ArrayList&lt;&gt;());
// 或使用
List&lt;Integer&gt; concurrentList = new CopyOnWriteArrayList&lt;&gt;();</code></pre>
                </div>
            </div>

            <!-- Subsection 5.4 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">5.4 避免内存泄漏</h3>
                <p class="text-gray-700 mb-4">当ArrayList持有大对象的引用，但实际上已不再需要使用时，应主动清除引用。</p>
                
                <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                    <pre><code class="text-sm">ArrayList&lt;LargeObject&gt; objects = new ArrayList&lt;&gt;();
// ... 添加大量大对象
// 使用完毕后
objects.clear(); // 清除所有引用，帮助GC回收</code></pre>
                </div>
            </div>

            <!-- Subsection 5.5 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">5.5 迭代时的修改问题</h3>
                <p class="text-gray-700 mb-4">在使用迭代器遍历ArrayList的同时修改集合，会抛出ConcurrentModificationException。</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-6">
                    <div class="warning-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">错误方式</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3, 4, 5));
for (Integer num : list) {
    if (num % 2 == 0) {
        list.remove(num); // 在迭代过程中修改集合，会抛出异常
    }
}</code></pre>
                        </div>
                    </div>
                    <div class="highlight-box p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-800 mb-2">正确方式一</h4>
                        <div class="code-block p-4 rounded-lg overflow-x-auto">
                            <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3, 4, 5));
Iterator&lt;Integer&gt; iterator = list.iterator();
while (iterator.hasNext()) {
    Integer num = iterator.next();
    if (num % 2 == 0) {
        iterator.remove(); // 使用迭代器的remove方法
    }
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="highlight-box p-4 rounded-lg">
                    <h4 class="font-semibold text-gray-800 mb-2">正确方式二：使用removeIf方法(Java 8+)</h4>
                    <div class="code-block p-4 rounded-lg overflow-x-auto">
                        <pre><code class="text-sm">ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3, 4, 5));
list.removeIf(num -&gt; num % 2 == 0);</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-blue-50 p-6 rounded-lg mb-16">
            <div class="flex items-start">
                <div class="flex-shrink-0 text-blue-500 mr-3 mt-1">
                    <i class="fas fa-star"></i>
                </div>
                <div>
                    <h3 class="article-title text-2xl font-bold text-blue-800 mb-3">关键要点总结</h3>
                    <ul class="list-disc pl-6 text-blue-800 space-y-2">
                        <li>ArrayList基于动态数组实现，支持快速随机访问</li>
                        <li>默认初始容量为10，扩容时大约增长为原容量的1.5倍</li>
                        <li>无参构造方法不会立即分配初始容量，延迟到第一次添加元素时</li>
                        <li>批量操作时预先分配足够容量可以显著提高性能</li>
                        <li>中间位置的插入和删除操作性能较差，应考虑使用LinkedList</li>
                        <li>ArrayList不是线程安全的，多线程环境需要同步或使用线程安全替代方案</li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-400 py-8">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <p class="text-white text-lg font-medium">技术小馆</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition duration-150 ease-in-out">http://www.yuque.com/jtostring</a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```